﻿#include "myprinterfun.h"
#include <QDebug>


MyPrinterFun::MyPrinterFun() {}


// // // // // // // // // // // // // //ByLabel// // // // // // // // // // // // // // // //

// 调用bartend打印
void MyPrinterFun::bartendPrint(){

}
// 调用byl打印
void MyPrinterFun::bylPrint(){

}

// bartend主窗口
HWND MyPrinterFun::bartendGetMainWin(){
    HWND hWnd=FindWindowA("BartendWindowClass", NULL);
    wchar_t windowTitle[256] = {0};
    int length = GetWindowTextW(hWnd, windowTitle, sizeof(windowTitle) / sizeof(windowTitle[0]));
    if(length>0&&(-1!=QString::fromStdWString(windowTitle).indexOf(".btw"))){
        return hWnd;
    }
    return NULL;
}
// 获取byl窗口的主窗口的句柄
// 参数一，可选，过滤窗口标题
HWND MyPrinterFun::bylGetMainWin(QString filter){
    HWND hwnd = NULL;
    while ((hwnd = FindWindowEx(NULL, hwnd, NULL, NULL)) != NULL) {
        WCHAR className[256];
        WCHAR windowTitle[256];
        // 获取窗口类名
        GetClassName(hwnd, className, sizeof(className));
        if (QString::fromStdWString(className) != "ByLabel") continue;
        // 获取窗口标题
        GetWindowText(hwnd, windowTitle, sizeof(windowTitle));
        if (-1==QString::fromStdWString(windowTitle).indexOf("BYLabel标签打印系统")) continue;
        if(!filter.isEmpty()&&-1==QString::fromStdWString(windowTitle).indexOf(filter)){
            continue;
        }
        break;
    }
    return hwnd;
    return NULL;
}

// 获取窗口标题
QString MyPrinterFun::GetWindowTitle(HWND hwnd){
    WCHAR windowTitle[256];
    // 获取窗口标题
    GetWindowText(hwnd, windowTitle, sizeof(windowTitle));
    return QString::fromStdWString(windowTitle);
}
// 获取byl窗口的打印的窗口的句柄
// 参数一，可选，过滤父窗口标题
HWND MyPrinterFun::bylGetPrinterWindow(QString filter){
    HWND hwnd = NULL;
    while ((hwnd = FindWindowEx(NULL, hwnd, NULL, NULL)) != NULL) {
        WCHAR className[256];
        WCHAR windowTitle[256];
        // 获取窗口类名
        GetClassName(hwnd, className, sizeof(className));
        if (QString::fromStdWString(className) != "TPrintForm") continue;
        // 获取窗口标题
        GetWindowText(hwnd, windowTitle, sizeof(windowTitle));
        if (QString::fromStdWString(windowTitle) != "打印设置") continue;
        if(!filter.isEmpty()){
            memset(windowTitle, 0, sizeof(windowTitle));
            GetWindowText(GetParent(hwnd), windowTitle, sizeof(windowTitle));
            if(-1==QString::fromStdWString(windowTitle).indexOf(filter)){
                continue;
            }
        }
        break;
    }
    return hwnd;
}

// 获取byl窗口的打印的窗口的子窗口按钮
HWND MyPrinterFun::bylGetPrinterWindowChild(HWND bylHwnd, QString wClass, QString wTitle){
    if(wClass.isEmpty()&&wTitle.isEmpty()) return NULL;
    HWND child_hwnd = GetWindow(bylHwnd, GW_CHILD);
    while (child_hwnd) {
        WCHAR className[256];
        WCHAR windowTitle[256];
        GetClassName(child_hwnd, className, sizeof(className));
        GetWindowText(child_hwnd, windowTitle, sizeof(windowTitle));
        if(!wClass.isEmpty()&&QString::fromStdWString(className)==wClass){
            break;
        }
        if(!wTitle.isEmpty()&&QString::fromStdWString(windowTitle)==wTitle){
            break;
        }
        child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT);
    }
    return child_hwnd;
}
void MyPrinterFun::bylGetPrinterWindowChild(HWND bylHwnd, QString wClass, QString wTitle, QList<HWND> &hwnd_list){
    if(wClass.isEmpty()&&wTitle.isEmpty()) return;
    HWND child_hwnd = GetWindow(bylHwnd, GW_CHILD);
    while (child_hwnd) {
        WCHAR className[256];
        WCHAR windowTitle[256];
        GetClassName(child_hwnd, className, sizeof(className));
        GetWindowText(child_hwnd, windowTitle, sizeof(windowTitle));
        if((!wClass.isEmpty()&&QString::fromStdWString(className)==wClass)||
            (!wTitle.isEmpty()&&QString::fromStdWString(windowTitle)==wTitle)){
            hwnd_list.push_back(child_hwnd);
        }
        child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT);
    }
    return;
}

// 获取byl窗口的打印的窗口的确定的按钮
HWND MyPrinterFun::bylGetPrinterWindowOK(HWND bylPrintHwnd){
    return bylGetPrinterWindowChild(bylPrintHwnd, "", "确定");
}

// 获取byl窗口的打印的窗口的三个输入框（起始，打印数量，副本数量）
QList<HWND> MyPrinterFun::bylGetPrinterWindowNum(HWND bylPrintHwnd){
    QList<HWND> hwnd_list;
    // 先获取TGroupBox的句柄
    HWND hwnd_TGroupBox=bylGetPrinterWindowChild(bylPrintHwnd, "TGroupBox", "");
    if(hwnd_TGroupBox==NULL) return QList<HWND>();
    // 获取TGroupBox中的TEdit
    bylGetPrinterWindowChild(hwnd_TGroupBox, "TEdit", "", hwnd_list);
    if(hwnd_list.size()!=3) return QList<HWND>();
    // 如果等于3，冒泡排序，根据控件的 y 位置排序
    for (int i = 0; i < hwnd_list.size() - 1; ++i) {
        for (int j = 0; j < hwnd_list.size() - i - 1; ++j) {
            RECT rect1, rect2;
            if (GetWindowRect(hwnd_list[j], &rect1) && GetWindowRect(hwnd_list[j + 1], &rect2)) {
                if (rect1.top > rect2.top) {
                    std::swap(hwnd_list[j], hwnd_list[j + 1]);
                }
            }
        }
    }
    return hwnd_list;
}
